//
//  KeyboardManager.swift
//  FearfulNotification
//
//  Created by 筱鹏 on 2018/3/23.
//  Copyright © 2018年 筱鹏. All rights reserved.
//

import UIKit

class KeyboardManager: NSObject {
    static let manager: KeyboardManager = KeyboardManager()
    var textView: UIView?
    var enable: Bool {
        willSet {
            if newValue {
                self.registerNotification()
            } else {
                self.unregisterNotification()
            }
        }
    }
    
    lazy var resignTapGes: UITapGestureRecognizer = {
        let ges = UITapGestureRecognizer(target: self, action: .resignTapSel)
        return ges
    }()
    
    private override init() {
        enable = false
        super.init()
    }
    
    private func registerNotification() {
        self.addNotification(beginName: UITextField.textDidBeginEditingNotification, endName: UITextField.textDidEndEditingNotification)
        self.addNotification(beginName: UITextView.textDidBeginEditingNotification, endName: UITextView.textDidEndEditingNotification)
    }
    
    private func unregisterNotification() {
        self.removeNotification(beginName: UITextField.textDidBeginEditingNotification, endName: UITextField.textDidEndEditingNotification)
        self.removeNotification(beginName: UITextView.textDidBeginEditingNotification, endName: UITextView.textDidEndEditingNotification)
    }
    
    private func addNotification(beginName: NSNotification.Name , endName: NSNotification.Name) {
        NotificationCenter.default.addObserver(self, selector: .textViewBeginSel, name: beginName, object: nil);
        NotificationCenter.default.addObserver(self, selector: .textViewEndSel, name: endName, object: nil)
    }
    
    private func removeNotification(beginName: NSNotification.Name , endName: NSNotification.Name) {
        NotificationCenter.default.removeObserver(self, name: beginName, object: nil)
        NotificationCenter.default.removeObserver(self, name: endName, object: nil)
    }
    
}

private extension Selector {
    static let textViewBeginSel = #selector(KeyboardManager.textViewBeginEdit(noti:))
    static let textViewEndSel = #selector(KeyboardManager.textViewEndEdit(noti:))
    static let resignTapSel = #selector(KeyboardManager.resignTabAction(sender:))
}

extension KeyboardManager {
    @objc func textViewBeginEdit(noti: Notification) {
        textView = noti.object as? UIView
        guard let view = textView else { return }
        if let window = view.window {
            window.addGestureRecognizer(self.resignTapGes)
        } else {
            ViewManager.window.addGestureRecognizer(self.resignTapGes)
        }
    }
    
    @objc func textViewEndEdit(noti: Notification) {
        guard let view = textView else { return }
        if let window = view.window {
            window.removeGestureRecognizer(self.resignTapGes)
        } else {
            ViewManager.window.removeGestureRecognizer(self.resignTapGes)
        }
        textView = nil
    }
    
    @objc func resignTabAction(sender: UITapGestureRecognizer) {
        if sender.state == .ended {
            guard let view = textView else { return }
            view.resignFirstResponder()
        }
    }
}
